from DeviceSystem.Groups.Group import GroupBase
print 'blah1'
from controller import KeyframeController
print 'blah2'
from sequencer import Sequencer

class Fader(GroupBase):
    def __init__(self, **kwargs):
        super(Fader, self).__init__(**kwargs)
        self._fade_time = kwargs.get('fade_time', 2.0)
        events = kwargs.get('events', {})
        self.member_values = {}
        self.controllers = {}
        self.calls = set()
        #self.clock = Clock(fps=30)
        #self.clock.add_callback(self.on_clock_tick)
        self.sequencer = Sequencer(duration=self.fade_time)
    @property
    def fade_time(self):
        return self._fade_time
    @fade_time.setter
    def fade_time(self, value):
        self._fade_time = float(value)
    def fade(self):
        self.sequencer.start()
    def add_controller(self, **kwargs):
        object = kwargs.get('object')
        start = kwargs.get('start')
        end = kwargs.get('end')
        c = KeyframeController(id=object.id, callback=self.on_controller_update)
        c.add_keyframes({0.0:start, 1.0:end})
        self.member_values.update({object.id:0})
        self.controllers.update({object.id:c})
        self.sequencer.add_controller(c)
        #self.calls.add(c.on_interval)
        #self.clock.add_controller(c)
        #self.clock.add_callback(c.on_interval)
        self.add_member(**{object.id:object})
#    def on_clock_tick(self, **kwargs):
#        #ms = kwargs.get('ms')
#        sec = kwargs.get('ms') / 1000
#        for call in self.calls:
#            call(sec)
#        if sec >= self.fade_time:
#            self.clock.stop()
        
    def on_controller_update(self, **kwargs):
        id = kwargs.get('id')
        value = kwargs.get('value')
        if id in self.controllers:
            self.member_values[id] = value
            self.members[id].update_user_value(self)
